package com.vincent.baseapp.util;

import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {
    private static FileUtils instance;

    public static FileUtils getInstance() {
        synchronized (FileUtils.class) {
            if (instance == null) {
                synchronized (FileUtils.class) {
                    instance = new FileUtils();
                }
            }
        }
        return instance;
    }

    private String SDPATH;

    private String projectFileDirPath;

    private String thumbnailDirPath;

    private String originalImgDirPath;

    public String getProjectFileDirPath() {
        return projectFileDirPath;
    }

    public String getSDPATH() {
        return SDPATH;
    }

    public String getThumbnailDirPath() {
        return thumbnailDirPath;
    }

    public String getOriginalImgDirPath() {
        return originalImgDirPath;
    }

    private FileUtils() {
        SDPATH = Environment.getExternalStorageDirectory() + File.separator;
        initAPPDir();
    }

    private void initAPPDir() {
        // 创建项目文件存储文件夹
        projectFileDirPath = createSDDir(SDPATH + "raspberry").getPath();
        // 创建图片缩略图存储文件夹
        File thumbnailFile = new File(projectFileDirPath + File.separator + "thumbnail");
        if (!isFileExist(projectFileDirPath + File.separator + "thumbnail")) {
            thumbnailFile.mkdir();
        }
        thumbnailDirPath = thumbnailFile.getPath();
        // 创建图片原图存储文件夹
        File originalImgFile = new File(projectFileDirPath + File.separator + "originalImg");
        if (!isFileExist(projectFileDirPath + File.separator + "originalImg")) {
            originalImgFile.mkdir();
        }
        originalImgDirPath = originalImgFile.getPath();
    }

    public String getImageSavePath(String fileName) {

        if (TextUtils.isEmpty(fileName)) {
            return null;
        }
        File folder = createSDFile(originalImgDirPath + File.separator + fileName);

        return folder == null ? null : folder.getAbsolutePath();
    }

    /**
     * 在原图文件夹中新建文件
     */
    public File createOriginalFile(String fileName) {
        return createSDFile(originalImgDirPath + File.separator + fileName);
    }

    /**
     * 在原图文件夹中获取文件
     */
    public File getOriginalFile(String fileName) {
        return createSDFile(originalImgDirPath + File.separator + fileName);
    }

    /**
     * 新建文件
     */
    public File createSDFile(String fileName) {

        File file = new File(fileName);
        if (!isFileExist(fileName)) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return file;
    }

    /**
     * 新建文件夹
     */
    public File createSDDir(String dirName) {
        File dir = new File(dirName);
        if (!isFileExist(dirName)) {
            try {
                dir.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return dir;
    }

    public boolean isFileExist(String fileName) {
        File file = new File(fileName);
        return file.exists();
    }

    public void deleteFile(String fileName) {
        File file = new File(fileName);
        file.delete();
    }

    public File writeToSDFromInput(String path, String fileName, InputStream input) {

        File file = null;
        OutputStream output = null;
        try {
            createSDDir(path);
            file = createSDFile(path + fileName);
            output = new FileOutputStream(file);
            byte buffer[] = new byte[1024];
            int len = -1;
            while ((len = input.read(buffer)) != -1) {
                output.write(buffer, 0, len);
            }
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                input.close();
                output.close();
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
        return file;
    }
}
